<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>🛡️ 饿了没</h1>
        <p>管理员系统</p>
      </div>
      
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label>管理员ID</label>
          <input 
            v-model="form.adminId" 
            type="text" 
            placeholder="请输入管理员ID"
            required
          />
        </div>
        
        <div class="form-group">
          <label>密码</label>
          <input 
            v-model="form.password" 
            type="password" 
            placeholder="请输入密码"
            required
          />
        </div>
        
        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
      
      <div class="demo-info">
        <p>演示账号：</p>
        <p>系统管理员 - ID: admin | 密码: 123456</p>
        <p>运营管理员 - ID: manager | 密码: 123456</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        adminId: '',
        password: ''
      },
      loading: false,
      error: ''
    }
  },
  methods: {
    async handleLogin() {
      this.loading = true
      this.error = ''
      
      try {
        // 验证输入
        if (!this.form.adminId || !this.form.password) {
          this.error = '请输入管理员ID和密码'
          return
        }
        
        // 简单的管理员验证逻辑
        // 在实际项目中，这应该调用后端API进行验证
        if (this.form.adminId === 'admin' && this.form.password === '123456') {
          // 登录成功
          this.$store.dispatch('login', {
            adminId: this.form.adminId,
            adminName: '系统管理员',
            token: 'admin-token-' + Date.now()
          })
          
          // 保存登录状态到localStorage
          localStorage.setItem('adminToken', 'admin-token-' + Date.now())
          localStorage.setItem('adminId', this.form.adminId)
          localStorage.setItem('adminName', '系统管理员')
          
          this.$router.push('/dashboard')
        } else if (this.form.adminId === 'manager' && this.form.password === '123456') {
          // 管理员登录
          this.$store.dispatch('login', {
            adminId: this.form.adminId,
            adminName: '运营管理员',
            token: 'manager-token-' + Date.now()
          })
          
          localStorage.setItem('adminToken', 'manager-token-' + Date.now())
          localStorage.setItem('adminId', this.form.adminId)
          localStorage.setItem('adminName', '运营管理员')
          
          this.$router.push('/dashboard')
        } else {
          this.error = '管理员ID或密码错误'
        }
      } catch (error) {
        console.error('登录失败:', error)
        this.error = '登录失败，请稍后重试'
      } finally {
        this.loading = false
      }
    },
    
    // 检查是否已登录
    checkLoginStatus() {
      const token = localStorage.getItem('adminToken')
      const adminId = localStorage.getItem('adminId')
      const adminName = localStorage.getItem('adminName')
      
      if (token && adminId) {
        this.$store.dispatch('login', {
          adminId,
          adminName: adminName || '管理员',
          token
        })
        this.$router.push('/dashboard')
      }
    }
  },
  
  mounted() {
    // 页面加载时检查登录状态
    this.checkLoginStatus()
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #FF6B6B 0%, #FF5252 100%);
}

.login-card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #FF6B6B;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.1);
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #FF6B6B 0%, #FF5252 100%);
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.error-message {
  color: #e74c3c;
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}

.demo-info {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
  text-align: center;
  font-size: 12px;
  color: #999;
}
</style>
